package dispatcher;
import gamer.Gamer;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
/**
 * 
 * @author LocoRocoBattleStein
 *
 */
	public class ClientDispatcher implements InterfaceDispatcher, Runnable{

		private Gamer gamer;
		private Socket socket;
		private Thread t;
		private BufferedWriter writer;
		private BufferedReader reader;
		
		public ClientDispatcher(String hostname, int port) {
			try {
				socket = new Socket(hostname, port);
				writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
				reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			t = new Thread(this);
			t.start();
		}
		
		public void run() {
			while(true) {
			try {
				char[] req = new char[100];
				int i=0;
				while ((req[i] = (char)reader.read()) != '\n') {
					System.out.println("New char : "+req[i]);
					i++;
				}
				char[] request = new char[i];
				for(int j = 0; j < i; j++) request[j] = req[j];
				String line = new String(request);
				this.processRequest(line);

			} catch (IOException e) {
				e.printStackTrace();
			}
			}
		}
		
		public void processRequest(String request) {
			String[] words = request.split(" ");
			System.out.println("REQUEST :"+request+" words "+words[0]);
			
			if(words[0].equals("startGame")) {
				this.startGame(Integer.parseInt(words[1]));
			}
			else if(words[0].equals("updateTower")) {
				if(words.length == 5) {
					this.updateTower(Integer.parseInt(words[1]), Integer.parseInt(words[2]), 
						      Integer.parseInt(words[3]), Integer.parseInt(words[4]));
				} else if(words.length == 6) {
					this.updateTower(Integer.parseInt(words[1]), Integer.parseInt(words[2]), 
						      Integer.parseInt(words[3]), Integer.parseInt(words[4]), Integer.parseInt(words[5]));
				}
			}
			else if(words[0].equals("updateDestBase")) {
				this.updateDestBase(Integer.parseInt(words[1]), Integer.parseInt(words[2]));
			}
		}
		
		public void setNetwork(String network) {
		}
		
		public void startGame(int idPlayer) {
			gamer.setId(idPlayer);
			gamer.startGame(idPlayer);
		}

		public void updateTower(int id, int idPlayer, int x, int y) {
			gamer.updateTower(id, idPlayer, x, y);
		}
		
		public void updateTower(int id, int rate, int range, int life, int velocity) {
			gamer.updateTower(id, rate, range, life, velocity);
		}
		
		public void updateDestBase(int src, int dest) {
			gamer.updateDestBase(src, dest);
		}
		
		public void newPlayer() {
			String request = "newPlayer\n";
			try {
				writer.write(request);
				writer.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		}

		public void setDestBase(int src, int dest) {
			String request = "setDestBase "+src+" "+dest+"\n";
			try {
				writer.write(request);
				writer.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		}

		public void upgradeTower(int id, int rate, int range, int life, int velocity) {
			String request = "upgradeTower "+id+" "+rate+" "+range+" "+life+" "+velocity+"\n";
			try {
				writer.write(request);
				writer.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		}

		public void addTower(int id, int idPlayer, int x, int y) {
			String request = "addTower "+id+" "+idPlayer+" "+x+" "+y+"\n";
			try {
				writer.write(request);
				writer.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		}


		public Gamer getGamer() {
			return gamer;
		}
		public void setGamer(Gamer gamer) {
			this.gamer = gamer;
		}

	}
